<?php
namespace app\shop\model;
use app\shop\model\EstateModel;
use think\image;
use think\Session;

class GoodsModel extends BaseModel
{
    protected $table = 'xst_goods';
    protected $rule = [
        'id' => '',
    ];
    protected $msg = [
        'id' => '',
    ];
    /*
     * 商品列表
     */
    public function getGoodsList()
    {
        $condition = 1;
        if(Session::get('isadmin')!=1)
        {
            $condition .=" and xst_goods.shop = ".Session::get('shop');
        }
        if(input('get.sub')){
            $keywords = input('keywords');
            if(!empty($keywords)){
                $condition .= " and xst_goods.goods like '%".$keywords."%'";
            }
        }
        $goodsList = $this->field('*,xst_goods.id as gid')
            ->where($condition)
            ->paginate(30,false,['query'=>request()->param()]);
        return $goodsList;
    }
    /*
     * 商品编辑
     */
    public function goodsEdit()
    {
        if(request()->isAjax()){
            $data = input("post.");
            $synopsis = $this->where('id',$data['id'])->find();
            if($data['synopsis']!=1){
                $data['synopsis'] = implode(',',$data['synopsis']);
                if($synopsis['synopsis']!=''){
                    $data['synopsis'] =$synopsis['synopsis'].",".$data['synopsis'];
                }
                else{
                    $data['synopsis'] =$data['synopsis'];
                }
            }else{
                if($synopsis['synopsis']==''){
                    return ['code'=>0,'msg'=>'商品详情不能为空'];
                }else{
                    $data['synopsis'] = $synopsis['synopsis'];
                }

            }
            if($data['img']!=1){
                $data['img'] = implode(',',$data['img']);
                if($synopsis['img']!='')
                    $data['img'] =$synopsis['img'].",".$data['img'];
                else{
                    $data['img'] =$data['img'];
                }
            }else{
                if($synopsis['img']==''){
                    return ['code'=>0,'msg'=>'商品详情不能为空'];
                }else{
                    $data['img'] = $synopsis['img'];
                }
            }
            $result = $this->allowField(true)->isUpdate(true)->save($data);
            if($result!==false){
                $goodsInfo = ['code'=>1,'msg'=>'修改成功'];
            }else{
                $goodsInfo = ['code'=>0,'msg'=>'修改失败'];
            }
        }else{
            $goodsInfo = $this->field('*,xst_goods.id as gid')
                ->where('xst_goods.id',input('id'))
                ->find();
            $goodsInfo['checkSynopsis'] = $goodsInfo['synopsis'];
            $goodsInfo['checkImg'] = $goodsInfo['img'];
          	if( $goodsInfo['synopsis']!=''){
             	$goodsInfo['synopsis'] = explode(',',$goodsInfo['synopsis']);
            }
            if( $goodsInfo['checkImg']!=''){
                $goodsInfo['img'] = explode(',',$goodsInfo['img']);
            }
        }
        return $goodsInfo;

    }
    /*
     * 商品添加
     */
    public function goodsAdd()
    {
        if (request()->isAjax()) {
            $data = input("post.");
            if($data['img']==1 || $data['synopsis'] ==1){
                return ['code'=>0,'msg'=>'图片不能为空'];
            }
            $data['img'] = implode(',',$data['img']);
            $data['synopsis'] = implode(',',$data['synopsis']);
            $data['shop'] = Session::get('shop');
            $data['type'] = Session::get('type');
            $query = $this->insert($data);
            if ($query) {
                $goodsInfo = ['code'=>1,'msg'=>'添加成功'];
            } else {
                $goodsInfo = ['code'=>0,'msg'=>'添加失败'];
            }
        }else{
            $goodsInfo = $this->getEModel($this->table);
            $goodsInfo['checkSynopsis'] = 0;
            $goodsInfo['checkImg'] = 0;
          
        }
        return $goodsInfo;

    }
    /*
     * 商品删除
     * */
    public function goodsDel()
    {
        $id = input("id");
        if(!empty($id)){
            $result = $this->where('id','in',$id)->delete();
            if($result){
                $status = ['code'=>1,'msg'=>'删除成功'];
            }else{
                $status = ['code'=>0,'msg'=>'删除失败'];
            }
        }else{
            $status = ['code'=>0,'msg'=>'请选择要删除的项'];
        }
        return $status;
    }
    /*
     * 商品简介
     * */
    public function lookSynopsis()
    {
        $id = input('id');
        $info = $this->getInfo(['id'=>$id],'synopsis');
      	$info = explode(',',$info["synopsis"]);
        return $info;
    }
    /*
     * 商品简介
     * */
    public function lookImg()
    {
        $id = input('id');
        $info = $this->getInfo(['id'=>$id],'img');
        $info = explode(',',$info["img"]);
        return $info;
    }

    // 图片上传和保存数据
    public function upload($files, $i)
    {
        $fileName = md5(time()) . $i;
        $fileRoute = ROOT_PATH . '/upload/goods/';
        // 创建文件夹
        if (!file_exists($fileRoute)) {
            mkdir($fileRoute);
        }
        $info = $files->move($fileRoute, $fileName);
        if ($info) {
            return  '/upload/goods/'.$info->getFilename();
        } else {
            // 上传失败获取错误信息
            return $info->getError();
        }
    }
    //编辑商品状态
    public function updateStatus()
    {
        $data = input('post.');
        $id = input('id');
        $result = $this->where('id',$id)->update($data);
        if($result){
            $status = ['code'=>1,'msg'=>'修改成功'];
        }else{
            $status = ['code'=>1,'msg'=>'修改失败'];
        }
        return $status;
    }
  //上传图片
    public function uploadImg()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        $savePath = ROOT_PATH.'/upload/goods/';
        $newname = time().rand(0,99999);
        $info = $file->move($savePath,$newname);
        if($info){
            $filename = '/upload/goods/'.$info->getFilename();
            exit(json_encode(['code' => 1, 'msg' => '上传成功', 'filename' => $filename]));
        }else{
            exit(json_encode(['code' => 0, 'msg' => '上传失败']));
        }
    }
    //删除图片
    public function delImgs()
    {
        $data = input('post.');
        $img = $this->where('id',$data['id'])->find();
        $imgs = explode(',',$img['synopsis']);
        for($i=0;$i<count($imgs);$i++){
            if($imgs[$i] == $data['src']){
                unset($imgs[$i]);
            }
        }
        $info = implode(',',$imgs);
        $result = $this->where(['id'=>$data['id']])->update(['synopsis'=>$info]);
        if($result != false){
            $status = ['code'=>1,'msg'=>'删除成功'];
        }else{
            $status = ['code'=>0,'msg'=>'删除失败'];
        }
        return $status;
    }
    //删除图片
    public function delImg()
    {
        $data = input('post.');
        $img = $this->where('id',$data['id'])->find();
        $imgs = explode(',',$img['img']);
        for($i=0;$i<count($imgs);$i++){
            if($imgs[$i] == $data['src']){
                unset($imgs[$i]);
            }
        }
        $info = implode(',',$imgs);
        $result = $this->where(['id'=>$data['id']])->update(['img'=>$info]);
        if($result != false){
            $status = ['code'=>1,'msg'=>'删除成功'];
        }else{
            $status = ['code'=>0,'msg'=>'删除失败'];
        }
        return $status;
    }
}
